package com.dkt.android.mvp.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.LayoutHelper
import com.dkt.android.R
import com.dkt.android.base.BaseHolder
import com.dkt.android.mvp.model.BannerBean
import com.dkt.android.widget.GlideImageLoader
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import com.youth.banner.Transformer


/**
 * @author: dtb
 * @email: wendaoyuqin1@gmail.com
 * @createTime: 19-3-12下午4:33
 */
class IndexBannerAdapter(
    private val list: MutableList<BannerBean>,
    private val mLayoutHelper: LayoutHelper
) : DelegateAdapter.Adapter<BaseHolder>() {


    var listener: ((str: BannerBean?) -> Unit?)? = null

    override fun getItemViewType(position: Int): Int {
        return 100
    }

    fun setOnListener(listener: (str: BannerBean?) -> Unit?) {
        this.listener = listener
    }

    override fun getItemCount(): Int = 1

    override fun onBindViewHolder(holder: BaseHolder, position: Int) {
        val pager = holder.getView<Banner>(R.id.rollpager)
        val images = mutableListOf<String>()
        val titles = mutableListOf<String>()
        for (l in list) {
            images.add(l.img?:"")
            titles.add(l.bannerName?:"")
        }

        pager.also {
            //        //设置banner样式
            it?.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
            //设置图片加载器
            it?.setImageLoader(GlideImageLoader())
            //设置图片集合
            it?.setImages(images)
            //设置banner动画效果
            it?.setBannerAnimation(Transformer.Default)
            //设置标题集合（当banner样式有显示title时）
            it?.setBannerTitles(titles)
            //设置自动轮播，默认为true
            it?.isAutoPlay(true)
            //设置轮播时间
            it?.setDelayTime(3500)
            //设置指示器位置（当banner模式中有指示器时）
            it?.setIndicatorGravity(BannerConfig.CENTER)
            //banner设置方法全部调用完毕时最后调用
            it?.start()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder = BaseHolder(
        LayoutInflater.from(parent.context)
            .inflate(R.layout.public_view_banner, parent, false)
    )

    override fun onCreateLayoutHelper(): LayoutHelper = mLayoutHelper
}